package com.heubu.mall.service.impl;


import com.heubu.mall.dao.CategoryMapper;
import com.heubu.mall.pojo.Category;
import com.heubu.mall.service.ICategoryService;
import com.heubu.mall.vo.CategoryVo;
import com.heubu.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.heubu.mall.consts.MallConst.ROOT_PARENT_ID;


/**
 * @Author Wangchuang
 * @Date: 2022/5/5 21:45
 * @Version 1.0
 * @Description:
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 查询出所有类目
     *
     * @return
     */
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        //查询父目录
        List<Category> categories = categoryMapper.selectAll();
    
        //lambda + stream
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::categoryTurnCategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());
    
        //查询子目录
        findSubCategory(categoryVoList, categories);
    
        return ResponseVo.success(categoryVoList);
    }
    
    
    /**
     * 根据类目id查询该类目下所有子类目id
     *
     * @param categoryId
     * @param result
     */
    @Override
    public void findSubCategoryId(Integer categoryId, Set<Integer> result) {
        //查询出所有类目
        List<Category> categories = categoryMapper.selectAll();
        
        findSubCategoryId(categoryId, result, categories);
    }
    
    /**
     * 转换
     * @param category
     * @return
     */
    private CategoryVo categoryTurnCategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }
    
    /**
     * 查找类目下的所有子目录
     * @param categoryVoList
     * @param categories
     */
    private void findSubCategory(List<CategoryVo> categoryVoList, List<Category> categories) {
        for (CategoryVo categoryVo : categoryVoList) {
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
        
            for (Category category : categories) {
                //如果查到内容，设置subCategory, 继续往下查
                if (categoryVo.getId().equals(category.getParentId())) {
                    CategoryVo subCategoryVo = categoryTurnCategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }
            
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setSubCategories(subCategoryVoList);
            
                findSubCategory(subCategoryVoList, categories);
            }
        }
    }
    
    private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categories) {
        for(Category category : categories) {
            if(category.getParentId().equals(id)) {
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(), resultSet, categories);
            }
        }
    }
    
}
